﻿<form id="myadd" class="layui-form layui-form-pane" lay-submit
   lay-filter="userForm" enctype="multipart/form-data">
	<input type="hidden" name="type" id="type" value="add">
	<input type="hidden" name="userno" id="userno">
	<div class="layui-form-item">
		<label class="layui-form-label">账号</label>
		<div class="layui-input-block">
			<input type="text" lay-verify="title" class="layui-input" name="userid" id="userid"
				   required placeholder="请输入账号">
		</div>
	</div>

	<div class="layui-form-item">
		<label class="layui-form-label">密码</label>
		<div class="layui-input-block">
			<input type="password" lay-verify="pass" class="layui-input" name="password"
				   id="password" required placeholder="">
		</div>
	</div>

	<div class="layui-form-item">
		<div class="layui-upload">
			<button type="button" class="layui-btn" id="photo" name="photo">
				<i class="layui-icon">&#xe67c;</i>上传头像
			</button>
			<div class="layui-upload-list">
				<img class="layui-upload-img" id="demo1">
				<p id="demoText"></p>
			</div>
		</div>
	</div>

	<div class="layui-form-item">
		<div class="layui-input-block">
			<button id="save" type="button" class="layui-btn" lay-submit lay-filter="formUser">立即提交</button>
			<button type="reset" class="layui-btn layui-btn-primary">重置</button>
		</div>
	</div>
</form>


<script>
	layui.use(['layer', 'form', 'upload', 'laydate'], function () {
		let layer = layui.layer;
		let form = layui.form;
		let upload = layui.upload;
		let laydate = layui.laydate;

		let myFile;
		upload.render({
			elem: '#photo',
			url: '/Upload/Image',
			// auto: false, // 选择文件后不自动上传
			// bindAction: '#save', // 指向一个按钮触发上传
			accept: 'images',
			choose: function (obj) {
				obj.preview(function (index, file, result) {
					$('#demo1').attr('src', result); // 图片链接（base64）
					myFile = file;

				});
			},
			before: function (obj) {
				// 预读本地文件示例，不支持ie8
				obj.preview(function (index, file, result) {
					$('#demo1').attr('src', result); // 图片链接（base64）
				});
			},
			done: function (res) {
				// 如果上传失败
				if (res.code > 0) {
					return layer.msg('上传失败');
				}
				// 上传成功
			},
			error: function () {
				// 演示失败状态，并实现重传
				var demoText = $('#demoText');
				demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
				demoText.find('.demo-reload').on('click', function () {
					uploadInst.upload();
				});
			}
		});

		form.on('submit(loginBtn)', function (data) {
			var user = data.field;
			$.post('/User/Login', user, function (res) {
				if ("0" == res.status)//如果成功则跳转到指定页面
					location.href = "/Home/Index";
				else {//失败则弹出失败提示
					layer.msg(res.msg, { icon: 5 });
				}
			});
			return false;
		});

	})

	function updatePerson(myFile) {
		let myForms = $('#myadd')[0];
		let formdatas = new FormData(myForms);
		formdatas.append("photo", myFile);
		console.log("updateFile", myFile)
		// 提示
		layer.confirm('确定要更新人员信息吗?', {
			icon: 3,
			title: '提示'
		},

			function (index) {
				$.ajax({
					method: 'post',
					url: '/TestAjax/UploadImgByForm',
					data: formdatas,
					contentType: false,
					processData: false,
					success: (data) => {
						if (data == '1') {
							// 提交成功
							layer.msg('更新成功', {
								icon: 1
							}, function () {
								// 当你在iframe页面关闭自身时
								var indexx = parent.layer.getFrameIndex(window.name); // 先得到当前iframe层的索引
								parent.layer.close(indexx); // 再执行关闭
								// 父页面刷新
								parent.location.reload();
							});
						} else {
							// 提交失败
							layer.msg('更新失败', {
								icon: 2
							}, function () {
								var indexx = parent.layer.getFrameIndex(window.name); // 先得到当前iframe层的索引
								parent.layer.close(indexx); // 再执行关闭

							})
						}

					},
					dataType: 'text'
				})

			});

		return false; // 阻止表单跳转。如果需要表单跳转，去掉这段即可。
	}

</script>